/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.controller.system.validator;

import cn.hg.base.jfinal.validator.BaseValidator;
import cn.hg.base.kit.PasswordKit;
import cn.hg.jfsa.core.SessionThreadLocal;
import cn.hg.jfsa.model.JfsaAdmin;
import com.jfinal.core.Controller;

/**
 * @author Golly
 *
 */
public class ChangePasswordValidator extends BaseValidator {

	/*
	 * (non-Javadoc)
	 *
	 * @see com.jfinal.validate.Validator#validate(com.jfinal.core.Controller)
	 */
	@Override
	protected void validate(Controller c) {
		if ("POST".equalsIgnoreCase(c.getRequest().getMethod())) {
			this.validateRequired("oldPassword", "flashMsg", "必须输入旧密码！");
			this.validateRequired("newPassword", "flashMsg", "必须输入新密码！");
			this.validateString("oldPassword", 6, 20, "flashMsg", "旧密码必须填写6到20长度的字符!");
			this.validateString("newPassword", 6, 20, "flashMsg", "新密码必须填写6到20长度的字符!");
			this.validateEqualField("newPassword", "confirmPassword", "flashMsg", "确认密码必须和新密码相同！");

			JfsaAdmin admin = SessionThreadLocal.getAdmin();
			if (!PasswordKit.validatePassword(admin.getSalt(), controller.getPara("oldPassword"), admin.getPassword())) {
				this.addError("flashMsg", "当前密码不正确！");
			}
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.jfinal.validate.Validator#handleError(com.jfinal.core.Controller)
	 */
	@Override
	protected void handleError(Controller c) {
	}

}
